import * as actions from '../actions/index';
import utils from 'utils/index';

const navKey = utils.getUrlSearchParam('navKey') || 'mine';

// 对页面prop 数据进行管理
export const initialState = {
  projects: [],
  search: '',
  isLoading: true,
  total: 0,
  currentPage: 1,
  navKey,
  order_by: 'last_activity_at',
  archived: false,
  repoCreatorIdentity: [],
  createRepositorySuccess: false,
  // 关闭导入代码库弹窗
  closeImportRepository: false,
  // 引导弹窗状态
  welcomeDialogStatus: null,
  // 计费接口-企业当前套餐
  enterprisePackage: '',
};
const defaultAction = {
  type: 'doNothing',
};

export default function index(state = initialState, action = defaultAction) {
  switch (action.type) {
    case actions.UPDATE_DATA:
      return { ...state, ...action.data };
    case actions.RECEIVE_PROJECTS:
      return {
        ...state,
        projects: [...action.data.list],
        total: action.data.amount,
        currentPage: action.data.currentPage,
        isLoading: false,
      };
    case actions.CHANGE_PROJECTS:
      return {
        ...state,
        projects: [...action.data.list],
        total: action.data.amount,
        currentPage: action.data.currentPage,
        isLoading: false,
      };
    case actions.CHANGE_SEARCH:
      return {
        ...state,
        search: action.data,
      };
    case actions.RECEIVE_REPO_SETTING: {
      const repo_creator_identity = utils.getCompatibleSettingData(
        action.data.repo_creator_identity,
      );
      return {
        ...state,
        repoCreatorIdentity: repo_creator_identity,
      };
    }
    case actions.SET_NAVKEY:
      return { ...state, navKey: action.data };
    case actions.ENTERPRISE_PACKAGE:
      return { ...state, enterprisePackage: action.data };
    default:
      return state;
  }
}
